<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>教师管理系统</title>
    <script src="/js/jquery-3.7.1.min.js"></script>
    <style>
        /* 保留原有的样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        header {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            text-align: center;
        }
        nav {
            width: 100px;
            float: left;
            background-color: #fff;
            padding: 20px;
            box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
        }
        nav a {
            display: block;
            margin: 10px 0;
            text-decoration: none;
            color: #4CAF50;
            padding: 10px;
            border-radius: 5px;
            transition: background 0.3s;
        }
        nav a:hover {
            background-color: #e7f7e7;
        }
        .search-container {
            margin-left: 220px;
            margin-top: 20px;
            margin-bottom: 20px;
            position: relative;
        }

        .search-container label {
            display: inline-block;
            margin-right: 10px;
        }

        .search-container input {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: calc(100% - 100px);
            margin-right: 0;
        }

        .search-container button {
            position: absolute;
            right: 20;
            top: 0;
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .search-container button:hover {
            background-color: #45a049;
        }


        .teachers-list {
            border-collapse: collapse;
            width: calc(100% - 220px);
            margin-left: 220px;
            background-color: white;
            box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
        }
        .teachers-list th, .teachers-list td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        .teachers-list th {
            background-color: #4CAF50;
            color: white;
        }
        .teachers-list tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .teachers-list tr:hover {
            background-color: #f1f1f1;
        }

        /* 按钮样式 */
        .table-button {
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin: 0 5px; /* 按钮之间的间距 */
        }

        .table-button.update {
            background-color: #4CAF50; /* 更新按钮颜色 */
            color: white;
            transition: background 0.3s;
        }

        .table-button.update:hover {
            background-color: #45a049; /* 更新按钮悬停效果 */
        }

        .table-button.delete {
            background-color: #f44336; /* 删除按钮颜色 */
            color: white;
            transition: background 0.3s;
        }

        .table-button.delete:hover {
            background-color: #d32f2f; /* 删除按钮悬停效果 */
        }


        /* 新增分页样式 */
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            margin-left: 220px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .pagination button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .pagination span {
            margin: 0 10px;
            line-height: 30px;
        }


    </style>
</head>
<body>

<header>
    <h1>教师管理系统222</h1>
</header>

<nav>
    <a href="#">首页</a>
    <a href="#">教师管理</a>
    <a href="#">课程管理</a>
    <a href="#">查询统计</a>
    <a href="#">系统设置</a>
</nav>

<div class="search-container">
    <label for="search">搜索教师:</label>
    <input type="text" id="search" placeholder="请输入教师姓名...">
    <button onclick="searchTeacher()">搜索</button>
</div>

<table class="teachers-list">
    <thead>
    <tr>
        <th>教师ID</th>
        <th>姓名</th>
        <th>性别</th>
        <th>电话</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="teachersTable">
    <!-- 教师数据将通过 JavaScript 动态填充 -->
    </tbody>
</table>

<div class="pagination">
    <button id="prevPage" onclick="changePage(-1)">上一页</button>
    <span id="pageInfo">第 <span id="currentPage">1</span> 页，共 <span id="totalPages">1</span> 页</span>
    <button id="nextPage" onclick="changePage(1)">下一页</button>

</div>

<script>
    let currentPage = 1;
    let totalPages = 1;
    const pageSize = 5; // 每页显示的记录数

    $(document).ready(function() {
        console.log("aaa")
        loadTeachers(currentPage);
    });

    function loadTeachers(page) {
        $.ajax({
            url: 'http://localhost:8080/teacher/getTeacherListByPage', // 替换为实际的API端点
            method: 'GET',
            data: {
                pageNumber: page,
                pageSize: pageSize,
                search:$('#search').val()

            },
            success: function(response) {
                console.log(response)
                const teachers = response.dataList;
                totalPages = response.totalPages;
                updateTable(teachers);
                updatePagination();
            },
            error: function(xhr, status, error) {
                console.error("加载教师数据失败:", error);
            }
        });
    }

    function updateTable(teachers) {
        const tbody = $('#teachersTable');
        tbody.empty();
        teachers.forEach(function(teacher) {
            tbody.append(`
                <tr>
                    <td>`+teacher.teacherID+`</td>
                    <td>`+teacher.name+`</td>
                    <td>`+teacher.gender+`</td>
                    <td>`+teacher.phone+`</td>
                    <td>`+teacher.email+`</td>
                    <td>
                        <button class="table-button update" onclick="updateTeacher(`+teacher+`)">
                            更新
                        </button>
                        <button class="table-button delete" onclick="deleteTeacher(`+teacher.teacherID+`)">
                            删除
                        </button>
                    </td>


                </tr>
            `);
        });
    }

    function updatePagination() {
        $('#currentPage').text(currentPage);
        $('#totalPages').text(totalPages);
        $('#prevPage').prop('disabled', currentPage === 1);
        $('#nextPage').prop('disabled', currentPage === totalPages);
    }

    function changePage(delta) {
        currentPage += delta;
        loadTeachers(currentPage);
    }

    function searchTeacher() {
        const searchQuery = $('#search').val();
        console.log(searchQuery)
        currentPage = 1; // 重置到第一页
        loadTeachers(currentPage);
    }

    function updateTeacher(teacherID) {
        // 这里可以添加更新逻辑，比如弹出一个更新表单
        console.log("更新教师ID:", teacherID);
        // 发送更新请求
        $.ajax({
            url: 'http://localhost:8080/teacher/update', // 替换为实际的API端点
            method: 'POST',
            data: {
                teacherID: teacherID,
                // 可以添加其它需要更新的数据
            },
            success: function(response) {
                console.log("更新成功:", response);
                loadTeachers(currentPage); // 刷新教师列表
            },
            error: function(xhr, status, error) {
                console.error("更新失败:", error);
            }
        });
    }

    function deleteTeacher(teacherID) {
        if (confirm("确定要删除该教师吗？")) {
            $.ajax({
                url: 'http://localhost:8080/teacher/delete', // 替换为实际的API端点
                method: 'DELETE',
                data: {
                    teacherID: teacherID
                },
                success: function(response) {
                    console.log("删除成功:", response);
                    loadTeachers(currentPage); // 刷新教师列表
                },
                error: function(xhr, status, error) {
                    console.error("删除失败:", error);
                }
            });
        }
    }
</script>

</body>
</html>